<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <button @click='showHide'>{{isShow?'消失':'显示'}}</button>
    <keep-alive>
      <common-head v-if='isShow'></common-head>
    </keep-alive>
  </div>
</body>
<script src="./vue.js"></script>
<script>
  Vue.component('CommonHead',{
    template: `
      <div>
        <h1>组件</h1>
        <input v-model='msg'/>
      </div>  
    `,
    data(){
      return{
        msg: '',
        timer: null
      }
    },
    beforeCreate() {
      console.log('beforeCreate触发')
    },
    created() {
      console.log('created触发')
    },
    beforeMount() {
      console.log('beforeMount触发')
    },
    mounted() {
      console.log('mounted触发');
      this.timer = setInterval(()=>{
        console.log(111);
      },1000)
    },activated(){
      console.log('组件激活了');
    },
    deactivated() {
      clearInterval(this.timer)
      console.log('组件停用了');
    },
    beforeDestroy() {
      console.log('卸载前');
    },
    destroyed() {
      console.log('卸载后');
    },
  })
  const vm = new Vue({
    el:'#app',
    data: {
      isShow: true
    },
    methods: {
      showHide(){
        this.isShow = !this.isShow
      }
    },
  })
</script>
</html>